home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Network Support Library
/
RoseWare - Network Support Library.iso
/
apidev
/
server.arc
/
SERVER.C
< prev
next >
Wrap
Text File
|
1989-11-29
|
5KB
|
126 lines
/*------------------------------------------------*
* Named Pipes server *
* *
* Written by: Stephen T. Bunch *
* API Development *
* *
* Date: July 11, 1989 *
* *
*------------------------------------------------*/
#include <stdio.h>
#define INCL_BASE
#include <os2.h>
#include <nwcalls.h>
#include <malloc.h>
#include "server.h"
#define CLIENT_NUM 4 /* number of threads to create (potentially the
number of concurrent clients also) */
#define STACK_SIZE 2000 /* stack size for each thread */
#define PIPENAME "\\PIPE\\TLAPIPE"
HPIPE pipeHandle[CLIENT_NUM];
/*----------------------------------------------------------------------*/
main(int argc,char *argv[])
{
int err, i;
void far Receiver(); /* address of thread routine */
TID threadID[CLIENT_NUM]; /* thread IDs */
USHORT far *stackPtr; /* ptr to thread's stack */
TLAReadIndex();
printf("\tWelcome to the TLA server program.\n");
printf("\tEnter 'down' to down this server.\n\n");
/* spin off the threads which will each initiate an instance */
for (i=0; i<CLIENT_NUM; i++) {
stackPtr = (WORD far *)malloc(STACK_SIZE) + (char)STACK_SIZE;
/* pass a parameter by putting the client number on the stack */
stackPtr--;
*stackPtr = i;
err = DosCreateThread(Receiver, &threadID[i], (char far *)stackPtr);
if (err)
printf("DosCreateThread Error : %d\n", err);
}
CleanUp();
}
/*----------------------------------------------------------------------*
* CleanUp performs the exiting procedures, disconnecting and closing *
* the pipes. *
*----------------------------------------------------------------------*/
CleanUp()
{
BYTE response[20];
int err, i;
/* wait until 'down' is indicated */
do {
gets(response);
} while (strcmp(response, "down") != 0);
TLACloseIndex();
/* disconnect all instances of the pipe */
DosEnterCritSec();
for (i=0; i<CLIENT_NUM; i++){
err = DosDisConnectNmPipe(pipeHandle[i]);
if (err)
printf("DosDisConnectNmPipe (Client %d) Error : %d\n", i, err);
}
/* close the pipe handels */
for (i=0; i<CLIENT_NUM; i++){
err = DosClose(pipeHandle[i]);
if (err)
printf("DosClose (Client %d) Error : %d\n", i, err);
}
DosExitCritSec();
DosExit(1, 0);
}
/*----------------------------------------------------------------------*
* Receiver is the server side of the IPC connection. It makes, reads, *
* writes, and closes the pipe until 'down' is entered. *
*----------------------------------------------------------------------*/
void far Receiver(ClientNum)
int ClientNum;
{
USHORT openMode = 0x0002; /* Duplex pipe */
USHORT pipeMode = 0x0504; /* Number of instances allowed */
LONG timeOut = 0; /* default time out value */
USHORT bytesWritten, bytesRead;
TID threadID;
USHORT err;
struct COMMBUFFER commBuffer;
/* create an instance of the named pipe */
err = DosMakeNmPipe(PIPENAME, &pipeHandle[ClientNum], openMode, pipeMode,
sizeof(struct COMMBUFFER), sizeof(struct COMMBUFFER), timeOut);
do {
/* wait for the user to connect to the named pipe */
err = DosConnectNmPipe(pipeHandle[ClientNum]);
printf("DosConnectNmPipe (Client %d) : %d\n", ClientNum, err);
/* Read from the pipe */
err = DosRead(pipeHandle[ClientNum], &commBuffer,
sizeof(struct COMMBUFFER), &bytesRead);
if (err)
printf("DosRead Error : %d\n", err);
if ((err = TLAServiceRequest(&commBuffer)) != 0) {
strcpy(commBuffer.record.explain,"The TLA requested could not be found");
}
err = DosWrite(pipeHandle[ClientNum], &commBuffer,
sizeof(struct COMMBUFFER), &bytesWritten);
if (err)
printf("DosWrite Error (Client %d) : %d\n", ClientNum, err);
DosSleep(500L);
/* user end of the pipe has gone away - close this instance */
err = DosDisConnectNmPipe(pipeHandle[ClientNum]);
} while (TRUE);
}